# -*- coding: utf-8 -*-
import scrapy,sys,os,json,pymongo
reload(sys).setdefaultencoding('UTF-8')
from scrapy.spiders import CrawlSpider
import signal
import logging
from pyquery import PyQuery as pq


class sbjSpider(scrapy.Spider):
        name = "sbj"
        allowed_domains = ["baidu.com"]

        def __init__(self):
            '''self.resultDict = {}
            signal.signal(signal.SIGINT,self.intHandler)
            signal.signal(signal.SIGTERM,self.termHandler)
            signal.signal(signal.SIGQUIT,self.quitHandler)'''

        def intHandler(self,sign,f):
            print "signal is ",signal

        def termHandler(self,sign,f):
            print "termHandler"

        def quitHandler(self,sign,f):
            print "quit"
            sys.exit(0)

        def store(filename,data):
            with open(filename, 'w') as f:  
                json.dump(data,f)

        def start_requests(self):#17000000
            for i in range(1,2000):
                next_page_url = "https://www.baidu.com/s?wd=%s" %(i)
                request = scrapy.Request(next_page_url,callback=self.parse)
                request.meta['regNO'] = i
                print "start xxx"
                yield request

        def parse(self,response):
            if response.status == 200:
                yield scrapy.Request("http://baidu.com",callback=self.parseContent)
                '''body = pq(response.body)
                regNO = response.meta['regNO']
                if len(body('.list_01 a'))>0:
                    intCls = pq(body('.list_01 a')[1]).text()
                    self.resultDict[regNO] = intCls
                    if len(self.resultDict)>100000:
                        store('r'+str(regNO)+'.json',self.resultDict)
                        self.resultDict.clear()
                else:
                    logging.info("商标%s不存在" %(regNO))'''
            else:
                logging.info("access faild code :%s" %(response.status))

        def parseContent(self,response):
            print "sss"

        def closed(self,reason):
            print 'process is %s' %(reason)